package com.andyliu.system.controller;


import com.andyliu.common.ResponseResult;
import com.andyliu.controller.BaseController;
import com.andyliu.interceptor.BusinessType;
import com.andyliu.log.Log;
import com.andyliu.system.dto.DeptDto;
import com.andyliu.system.entity.Dept;
import com.andyliu.system.entity.Ztree;
import com.andyliu.system.service.IDeptService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author andyLiu
 * @since 2021-08-10
 */
@Slf4j
@Controller
@RequestMapping("/system/dept")
@Api(value = "部门表控制器",tags = "部门表操作接口")
public class DeptController extends BaseController {
    @Autowired
    private IDeptService service;
    /**
    * 跳转到列表页
    */
    @GetMapping()
    @ApiIgnore
    public String toData(){
        log.debug("跳转到列表页");
        return"system/Dept/data";
    }
    /**
    * 异步加载分页数据
    */
    @PostMapping("data")
    @ResponseBody
    @ApiOperation(value = "部门表树形表格数据接口",notes = "根据查询返回树形表格数据",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name="obj",value="用于分装实体的查询条件",required = false)
    })
    @ApiResponses({ @ApiResponse(code = 200, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常"),
            @ApiResponse(code = 501, message = "权限不足")
    })
    @Log(moduleName = "部门查询",businessType = BusinessType.QUERY)
    public ResponseResult data( Dept obj){
            log.debug("查询Dept数据");
        List<Dept> depts = service.selectDepts(obj);
        return ResponseResult.success(depts);
    }
    /**
    * 跳转到新增编辑页面
    */
    @GetMapping("/edit/{id}")
    @ApiIgnore
    public String toEidt(@PathVariable Integer id,ModelMap mmap){
        if(id!=-1){
            Dept dept=service.getById(id);
            mmap.put("dept" ,dept);
        }
        return"system/Dept/edit";
    }

    @GetMapping("toTree")
    @ApiIgnore
    public String toTree(){
        return "system/Dept/tree";
    }

    @GetMapping("/info/{id}")
    @ResponseBody
    @ApiOperation(value = "根据主键获取部门表数据接口",notes = "根据主键获取实体数据",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "主键",required = true,type = "Integer")
    })
    @ApiResponses({ @ApiResponse(code = 200, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常"),
            @ApiResponse(code = 501, message = "权限不足")
    })
    public ResponseResult toEidt(@PathVariable Integer id){
        log.debug("查看部门表的数据,id为"+id);
        DeptDto obj = service.queryDeptById(id);
        return ResponseResult.success(obj);
    }

    /**
    * 异步保存或新增
    */
    @PostMapping("save")
    @ResponseBody
    @ApiOperation(value = "保存或更新部门表数据接口",notes = "如果实体类中有主键为更新，否则为保存",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user",value = "用户信息表实体",required = true,type = "Integer")
    })
    @ApiResponses({ @ApiResponse(code = 200, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常"),
            @ApiResponse(code = 501, message = "权限不足")
    })
    @Log(moduleName = "部门保存",businessType = BusinessType.INSERT)
    public ResponseResult save(@RequestBody Dept dept){
        log.debug("保存部门表的数据");
        if(dept.getDeptId()!=null){
            dept.setUpdateTime(LocalDateTime.now());
            boolean status=service.updateById(dept);
            if(status){
            return ResponseResult.success();
            }else{
            return ResponseResult.error();
            }
        }
        dept.setCreateTime(LocalDateTime.now());

        return service.save(dept)==true?
        ResponseResult.success()
        :ResponseResult.error();
    }
    /**
    * 删除或批量删除请求
    */
    @DeleteMapping("delete")
    @ResponseBody
    @ApiOperation(value = "批量删除部门表数据接口",notes = "批量删除",httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids",value = "需要删除的主键的数组",required = true,type = "Integer")
    })
    @ApiResponses({ @ApiResponse(code = 200, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常"),
            @ApiResponse(code = 501, message = "权限不足")
    })
    @Log(moduleName = "部门删除",businessType = BusinessType.DELETE)
    public boolean delete(Integer[] ids){
        log.debug("删除部门表的数据,id为"+ids);
        return service.removeByIds(Arrays.asList(ids));
    }
    /**
    * 单个删除
    */
    @DeleteMapping("delete/{id}")
    @ResponseBody
    @ApiOperation(value = "删除部门表数据接口",notes = "单个删除，根据主键删除",httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "需要删除主键",required = true,type = "Integer")
    })
    @ApiResponses({ @ApiResponse(code = 200, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常"),
            @ApiResponse(code = 501, message = "权限不足")
    })
    @Log(moduleName = "部门删除",businessType = BusinessType.DELETE)
    public ResponseResult delete(@PathVariable Integer id){
        log.debug("删除部门表的数据,id为"+id);
        return service.removeById(id)==true?ResponseResult.success("删除成功"):ResponseResult.error("删除失败");
    }

    /**
     * 部门树异步数据加载
     * @return
     */
    @GetMapping("treeData")
    @ResponseBody
    @ApiOperation(value = "部门树接口数据",notes = "获取部门树",httpMethod = "GET")
    @Log(moduleName = "部门树加载",businessType = BusinessType.QUERY)
    public List<Ztree> treeData(Dept dept){
        val data = service.queryDeptForTree(dept);
        return data;
    }

}


